package oa.hleast.common.utils;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * @author 302
 * 基于jackson的json工具
 */
@Slf4j
public class JsonUtil {
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();


    private static final String DATETIME_STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";

    static {
        //对象的所有字段全部列入
        OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.ALWAYS);
        //取消默认转换timestamps形式
        OBJECT_MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        //忽略空Bean转json的错误
        OBJECT_MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        //所有的日期格式都统一为以下的样式，即yyyy-MM-dd HH:mm:ss
        OBJECT_MAPPER.setDateFormat(new SimpleDateFormat(DATETIME_STANDARD_FORMAT));
        //忽略 在json字符串中存在，但是在java对象中不存在对应属性的情况。防止错误
        OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//        OBJECT_MAPPER.setPropertyNamingStrategy(PropertyNamingStrategy.)
    }

    /**
     * 将对象序列化为JSON字符串
     */
    public static <T> String obj2String(T obj) {
        if (obj == null) {
            return null;
        }
        try {
            if (obj instanceof String) {
                return (String) obj;
            } else {
                return OBJECT_MAPPER.writeValueAsString(obj);
            }
        } catch (JsonProcessingException e) {
            log.warn("Parse Object to String ERROR", e);
            return null;
        }
    }

    public static <T> String obj2StringPretty(T obj) {
        if (obj == null) {
            return null;
        }
        try {
            if (obj instanceof String) {
                return (String) obj;
            } else {
                return OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
            }
        } catch (JsonProcessingException e) {
            log.warn("Parse Object to String ERROR", e);
            return null;
        }
    }

    /**
     * 将JSON字符串反序列化为对象
     */
    public static <T> T string2Obj(String str, Class<T> clazz) {
        if (StrUtil.isEmpty(str) || clazz == null) {
            return null;
        }

        try {
            if (clazz.equals(String.class)) {
                return (T) str;
            } else {
                return OBJECT_MAPPER.readValue(str, clazz);
            }
        } catch (IOException e) {
            log.warn("Parse String to Object ERROR", e);
            return null;
        }
    }

    /**
     * 将JSON字符串反序列化为对象 下划线转驼峰
     */
    public static <T> T string2ObjWithLower(String str, Class<T> clazz) {
        if (StrUtil.isEmpty(str) || clazz == null) {
            return null;
        }
        ObjectMapper OBJECT_MAPPER_LOWER_CASE = new ObjectMapper();
        OBJECT_MAPPER_LOWER_CASE.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        try {
            if (clazz.equals(String.class)) {
                return (T) str;
            } else {
                return OBJECT_MAPPER_LOWER_CASE.readValue(str, clazz);
            }
        } catch (IOException e) {
            log.warn("Parse String to Object ERROR", e);
            return null;
        }
    }

    public static <T> T string2Collection(String str, TypeReference<T> typeReference) {
        if (StrUtil.isEmpty(str) || typeReference == null) {
            return null;
        }

        try {
            if (typeReference.getType().equals(String.class)) {
                return (T) str;
            } else {
                return OBJECT_MAPPER.readValue(str, typeReference);
            }
        } catch (IOException e) {
            log.warn("Parse String to Collection ERROR", e);
            return null;
        }
    }

    public static <T> T stringWithLowerCase(String str, TypeReference<T> typeReference) {
        if (StrUtil.isEmpty(str) || typeReference == null) {
            return null;
        }
        ObjectMapper OBJECT_MAPPER_LOWER_CASE = new ObjectMapper();
        OBJECT_MAPPER_LOWER_CASE.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        try {
            if (typeReference.getType().equals(String.class)) {
                return (T) str;
            } else {
                return OBJECT_MAPPER_LOWER_CASE.readValue(str, typeReference);
            }
        } catch (IOException e) {
            log.warn("Parse String to Collection ERROR", e);
            return null;
        }
    }

    public static <T> T string2Obj(String str, Class<?> collectionClazz, Class<?>... elementClazzes) {
        JavaType javaType = OBJECT_MAPPER.getTypeFactory().constructParametricType(collectionClazz, elementClazzes);
        try {
            return OBJECT_MAPPER.readValue(str, javaType);
        } catch (IOException e) {
            log.warn("Parse String to Collection ERROR", e);
            return null;
        }
    }


}
